$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;

    $(".head .refresh").on("click", function () {
        window.location.href = "divide.html";
    });

    $("#goods_id").focus();

    $("div.page").on("click", "span.personal", function() {
        localStorage.removeItem("divideMachine");
        focus = false;
        $("#goods_id").blur();
    });

    $(".panel-overlay").on("click", function () {
        focus = true;
        setTimeout(function () {
            $("#goods_id").val("").focus();
        }, 1000);
    });

    //扫描任务单号
    $("#goods_id").on("keyup", function (event) {
        if (event.keyCode == "13") {
            var number = $(this).val();
            if (!number) return $alert("回倒任务单号不可为空");
            getGobacktaskorders(number);
        }
    });

    //获取回倒单列表
    getGobacktaskorders();

    function getGobacktaskorders(number) {
        //加载提示符 显示
        $(".content .list-block ul#work_list .infinite-scroll-preloader").show();
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/divide/getGobacktaskorders',
            data: {
                number: number
            },
            dataType: 'json',
            json: 'callback',
            cache: false,
            timeout: 60000,
            success: function (res) {
                if (res.success) {
                    var data = res.result;
                    //清空页面中的回倒单
                    $("#work_list").find("li").remove();
                    //隐藏提示符 显示
                    $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                    for (var i = 0; i < data.length; i++) {
                        var data_html = '';
                        data_html += '<li>' +
                            '<p class="li_title">回倒单号:<span class="sp work_code">' + data[i].number + '</span><span class="icon iconfont icon-right pull-right"></span></p>' +
                            '<div class="row no-gutter li_content"><div class="col-100">' +
                            '<div class="details">' +
                            '<p class="p_none"><span class="id">' + (data[i]._id) + '</span></p>' +
                            '<p><span>排产单号:</span><span class="sp workplanNumber">' + data[i].workplanNumber + '</span></p>' +
                            '<p><span>名称:</span><span class="name sp">' + (data[i].inventoryName ? data[i].inventoryName : "") + '</span></p>' +
                            '<p><span>规格:</span><span class="specification sp">' + (data[i].inventorySpecification ? data[i].inventorySpecification : "") + '</span>' +
                            '<p><span>段长标注:</span><span class="segmentLength sp">' + (data[i].segmentLength ? data[i].segmentLength : "") + '</span>' +
                            '<span>单根米长:</span><span class="orderLength sp">' + (data[i].orderLength ? data[i].orderLength : "") + '</span>' +
                            // '<span>增加米长:</span><span class="addLength sp">' + (data[i].addLength ? data[i].addLength : "") + '</span>' +
                            '</p>' +
                            '<p><span>盘具:</span><span class="disctool sp">' + (data[i].disctool ? data[i].disctool : "") + '</span></p>' +
                            '<p><span>创建时间:</span><span class="createTime sp">' + (data[i].createTime ? data[i].createTime : "") + '</span></p>';
                        if (data[i].comment) {
                            data_html += '<p><span>备注:</span><span class="sp comment">' + data[i].comment + '</span></p>';
                        }
                        data_html += '</div></div></div></li>';
                        $("#work_list").append(data_html);
                    }
                    //改变 共计 数
                    $(".content div.ul_foot span.sp_js").html($("#work_list li").length);
                } else {
                    $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                    $alert(res.message);
                }
            },
            error: function (error) {
                $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                if (error.status == 0) {
                    //loading_hide();
                    $alert("请求超时，请重新操作！");
                } else {
                    //loading_hide();
                    $alert(error.responseText);
                }
            }
        });
    }

    //点击 回倒单箭头
    $(".content #work_list").on("click", "li .li_title", function () {
        var id = $(this).parent().find("span.id").html();
        localStorage.setItem("divideId", id);
        window.location.href = "divide_one.html";
    });

});